package com.tencreat.service.mall.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.common.utils.SecurityUtils;
import com.tencreat.domain.mall.MallExpressTemplate;
import com.tencreat.domain.mall.MallExpressZone;
import com.tencreat.mapper.mall.MallExpressTemplateMapper;
import com.tencreat.service.mall.IMallExpressTemplateService;
import com.tencreat.service.mall.IMallExpressZoneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 快递模板Service业务层处理
 *
 * @author tencreat
 * @date 2022-01-06
 */
@Service
public class MallExpressTemplateServiceImpl extends ServiceImpl<MallExpressTemplateMapper, MallExpressTemplate> implements IMallExpressTemplateService {

    @Autowired
    private IMallExpressZoneService mallExpressZoneService;

    /**
     * 查询快递模板
     *
     * @param id 快递模板ID
     * @return 快递模板
     */
    @Override
    public MallExpressTemplate selectMallExpressTemplateById(ReqIdParam id) {
        return baseMapper.selectById(id.getId());
    }

    /**
     * 查询快递模板列表
     *
     * @param mallExpressTemplate 快递模板
     * @return 快递模板
     */
    @Override
    public List<MallExpressTemplate> selectMallExpressTemplateList(MallExpressTemplate mallExpressTemplate) {
        return baseMapper.selectList(Wrappers.query(mallExpressTemplate));
    }

    /**
     * 新增快递模板
     *
     * @param mallExpressTemplate 快递模板
     * @return 结果
     */
    @Override
    public int insertMallExpressTemplate(MallExpressTemplate mallExpressTemplate) {
        mallExpressTemplate.setCreateTime(LocalDateTime.now());
        mallExpressTemplate.setCreateBy(SecurityUtils.getLoginUser().getUser().getUserName());
        mallExpressTemplate.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        int insert = baseMapper.insert(mallExpressTemplate);
        List<Long> zoneIds = mallExpressTemplate.getZoneIds();
        for (Long id : zoneIds){
            MallExpressZone expressZone = mallExpressZoneService.getById(id);
            expressZone.setTemplateId(mallExpressTemplate.getId());
            mallExpressZoneService.updateById(expressZone);
        }
        return insert;
    }

    /**
     * 修改快递模板
     *
     * @param mallExpressTemplate 快递模板
     * @return 结果
     */
    @Override
    public int updateMallExpressTemplate(MallExpressTemplate mallExpressTemplate) {
        mallExpressTemplate.setUpdateTime(LocalDateTime.now());
        return baseMapper.updateById(mallExpressTemplate);
    }

    /**
     * 批量删除快递模板
     *
     * @param ids 需要删除的快递模板ID
     * @return 结果
     */
    @Override
    public int deleteMallExpressTemplateByIds(ReqIdsParam ids) {
        return baseMapper.deleteBatchIds(ids.getId());
    }

    /**
     * 删除快递模板信息
     *
     * @param id 快递模板ID
     * @return 结果
     */
    @Override
    public int deleteMallExpressTemplateById(ReqIdParam id) {
        return baseMapper.deleteById(id.getId());
    }
}
